/**
 * BladeX Commercial License Agreement
 * Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
 * <p>
 * Use of this software is governed by the Commercial License Agreement
 * obtained after purchasing a license from BladeX.
 * <p>
 * 1. This software is for development use only under a valid license
 * from BladeX.
 * <p>
 * 2. Redistribution of this software's source code to any third party
 * without a commercial license is strictly prohibited.
 * <p>
 * 3. Licensees may copyright their own code but cannot use segments
 * from this software for such purposes. Copyright of this software
 * remains with BladeX.
 * <p>
 * Using this software signifies agreement to this License, and the software
 * must not be used for illegal purposes.
 * <p>
 * THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
 * not liable for any claims arising from secondary or illegal development.
 * <p>
 * Author: Chill Zhuang (bladejava@qq.com)
 */
package org.springblade.modules.auth.granter;

import lombok.AllArgsConstructor;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.redis.cache.BladeRedis;
import org.springblade.modules.auth.enums.UserEnum;
import org.springblade.modules.auth.provider.ITokenGranter;
import org.springblade.modules.auth.provider.TokenParameter;
import org.springblade.modules.auth.utils.TokenUtil;
import org.springblade.modules.sms.AliSmsTemplate;
import org.springblade.modules.sms.model.SmsCode;
import org.springblade.modules.system.pojo.entity.Tenant;
import org.springblade.modules.system.pojo.entity.UserInfo;
import org.springblade.modules.system.service.IAgentUserService;
import org.springblade.modules.system.service.ITenantService;
import org.springframework.stereotype.Component;

/**
 * SmsTokenGranter
 *
 * @author BladeX
 */
@Component
@AllArgsConstructor
public class SmsTokenGranter implements ITokenGranter {
	public static final String GRANT_TYPE = "sms_token";

	private final IAgentUserService userService;
	private final ITenantService tenantService;
	private final BladeRedis bladeRedis;
	private final AliSmsTemplate aliSmsTemplate;

	@Override
	public UserInfo grant(TokenParameter tokenParameter, Integer state) {

		String tenantId = tokenParameter.getArgs().getStr("tenantId");
		String phone = tokenParameter.getArgs().getStr("phone");
		String id = tokenParameter.getArgs().getStr("id");
		String value = tokenParameter.getArgs().getStr("value");
		org.springblade.modules.sms.model.SmsCode smsCode = new SmsCode();
		smsCode.setId(id);
		smsCode.setPhone(phone);
		smsCode.setValue(value);
		// 短信验证默认通过，等短信开通后配合短信发送一起放开
		boolean valid = aliSmsTemplate.validateMessage(smsCode);
		UserInfo userInfo = null;
		// 验证短信验证码是否通过
		if (valid) {
			// 获取租户信息
			Tenant tenant = tenantService.getByTenantId(tenantId);
			if (TokenUtil.judgeTenant(tenant)) {
				throw new ServiceException(TokenUtil.USER_HAS_NO_TENANT_PERMISSION);
			}
			// 获取用户类型
			String userType = tokenParameter.getArgs().getStr("userType");
			// 根据不同用户类型调用对应的接口返回数据，用户可自行拓展
			if (userType.equals(UserEnum.WEB.getName())) {
				userInfo = userService.userInfo(tenantId, phone, UserEnum.WEB);
			} else if (userType.equals(UserEnum.APP.getName())) {
				userInfo = userService.userInfo(tenantId, phone, UserEnum.APP);
			} else {
				userInfo = userService.userInfo(tenantId, phone, UserEnum.OTHER);
			}
		}
		return userInfo;
	}
}
